package com.control;

import com.pojo.Course;
import com.service.impl.CourseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/course")
@CrossOrigin(allowCredentials = "true")
public class CourseControl {

    final private CourseServiceImpl courseService;

    @Autowired
    public CourseControl(CourseServiceImpl courseService){
        this.courseService = courseService;
    }

    @RequestMapping("/get_course_by_id")
    public Course getCourseById(String id){
        Course course;
        try {
            course = courseService.findCourseById(id).get(0);
        } catch (Exception e){
            return null;
        }
        return course;
    }

    @RequestMapping("/add_course")
    public String addCourse(String creatorId, String courseName, String className){
        return courseService.addCourse(creatorId, courseName, className);
    }

    @RequestMapping("/join_course")
    public String joinCourse(String courseCode, String studentId){
        return courseService.joinCourseByCode(courseCode, studentId);
    }

    @RequestMapping("/drop_out_course")
    public String dropOutCourse(String studentId, String courseId){
        return courseService.dropOutCourse(studentId, courseId);
    }
}
